﻿using System;
using System.IO;

namespace Anderson.NetLobby.Common
{
    public class ByteStream : IDisposable
    {
        private MemoryStream stream;
        private BinaryWriter writer;
        private BinaryReader reader;

        public ByteStream(byte[] bytes)
        {
            stream = new MemoryStream(bytes);

            writer = new BinaryWriter(stream);
            reader = new BinaryReader(stream);
        }

        public ByteStream()
        {
            stream = new MemoryStream();

            writer = new BinaryWriter(stream);
            reader = new BinaryReader(stream);
        }

        public void Close()
        {
            try
            {
                if (null != writer)
                {
                    writer.Close();
                    writer = null;
                }

                if(null != reader)
                {
                    reader.Close();
                    reader = null;
                }

                if (null != stream)
                {
                    stream.Close();
                    stream = null;
                }                
            }
            catch (Exception e)
            {

            }
        }

        public void Dispose()
        {
            Close();
        }

        public byte[] GetBytes()
        {
            byte[] bytes = new byte[stream.Length];

            Buffer.BlockCopy(stream.GetBuffer(), 0, bytes, 0, (int)stream.Length);

            return bytes;
        }

        #region Seek
        public void Seek(int position = 0)
        {
            if (null != stream)
            {
                stream.Position = position;
            }
        }
        #endregion

        #region Reader
        public bool ReadBoolean()
        {
            return reader.ReadBoolean();
        }
        public byte ReadByte()
        {
            return reader.ReadByte();
        }
        public byte[] ReadBytes(int count)
        {
            return reader.ReadBytes(count);
        }
        public double ReadDouble()
        {
            return reader.ReadDouble();
        }
        public int ReadInt32()
        {
            return reader.ReadInt32();
        }
        public long ReadInt64()
        {
            return reader.ReadInt64();
        }
        public float ReadSingle()
        {
            return reader.ReadSingle();
        }
        public string ReadString()
        {
            return reader.ReadString();
        }
        public uint ReadUInt32()
        {
            return reader.ReadUInt32();
        }
        #endregion

        #region Writer
        public void Write(int value)
        {
            writer.Write(value);
        }
        public void Write(uint value)
        {
            writer.Write(value);
        }
        public void Write(byte value)
        {
            writer.Write(value);
        }
        public void Write(bool value)
        {
            writer.Write(value);
        }
        public void Write(string value)
        {
            writer.Write(value);
        }
        public void Write(byte[] value)
        {
            writer.Write(value);
        }
        public void Write(double value)
        {
            writer.Write(value);
        }
        public void Write(float value)
        {
            writer.Write(value);
        }
        public void Write(long value)
        {
            writer.Write(value);
        }
        #endregion
    }
}
